import { AstContext } from "../../../context";

export function createToTitle(ast: AutoJs.UiObject, ctx: AstContext, key: string) {
    return (title: string) => {
        const value = ctx.get(key)
        if (title === value) return true;
        const node = ctx.dfsOne(ast, it => it.text() === title || it.desc() === title)
        if (node) {
            if (node.clickable()) {
                const res = node.click()
                node.recycle()
                return res;
            } else {
                const clickable = ctx.findInParent(node, it => it.clickable())
                if (clickable) {
                    const res = clickable.click()
                    clickable.recycle()
                    return res;
                }
            }
        }
        return false;
    }
}
